/*
 * @Description: 过滤条
 * @Author: iamsmiling
 * @Date: 2021-06-08 06:59:37
 * @LastEditTime: 2021-06-13 18:14:06
 */

import 'package:credit_business_circle/R/R.dart';
import 'package:credit_business_circle/components/button/roatet_button.dart';
import 'package:credit_business_circle/components/modal/popdown_modal.dart';
import 'package:credit_business_circle/pages/city/city_select_page.dart';
import 'package:credit_business_circle/pages/industry/industry_page.dart';
import 'package:flutter/material.dart';

class FilterOptionEntity {
  late String icon;
  late String label;
  int index = 0;
  late Function()? onTap;
  FilterOptionEntity(
      {this.icon = '', required this.label, this.index = 0, this.onTap});
}

class FilterBar extends StatefulWidget {
  final Future? Function()? onLastestPublish;
  final Future? Function()? onLastestUpdate;
  final Future? Function()? onIndustrySelect;
  final Future? Function()? onCitySelect;
  const FilterBar(
      {Key? key,
      this.onLastestPublish,
      this.onLastestUpdate,
      this.onIndustrySelect,
      this.onCitySelect})
      : super(key: key);

  @override
  _FilterBarState createState() => _FilterBarState();
}

class _FilterBarState extends State<FilterBar> {
  List<FilterOptionEntity> options1 = [];

  List<FilterOptionEntity> options2 = [];
  int currentIndex = 0;

  select(FilterOptionEntity option) {
    if (option.onTap != null) {
      option.onTap!();
    }
    setState(() {
      currentIndex = option.index;
    });
  }

  @override
  void initState() {
    super.initState();
    options1 = [
      FilterOptionEntity(
          label: "最新发布", index: 0, onTap: widget.onLastestPublish),
      FilterOptionEntity(
          label: "最近更新", index: 1, onTap: widget.onLastestUpdate),
    ];
    options2 = [
      FilterOptionEntity(
          label: "所属行业",
          icon: R.image.dropdown,
          index: 2,
          onTap: () {
            return showModalPopdown(context, builder: (BuildContext context) {
              return ClipRRect(
                borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(R.dimen.sp13),
                    bottomRight: Radius.circular(R.dimen.sp13)),
                child: Container(
                  height: R.dimen.height * .8,
                  child: IndustryPage(),
                ),
              );
            }, settings: RouteSettings(), offset: 0);
          }),
      FilterOptionEntity(
          label: "合作地区",
          icon: R.image.dropdown,
          index: 3,
          onTap: () {
            return showModalPopdown(context, builder: (BuildContext context) {
              return CitySelectPage();
            }, settings: RouteSettings(), offset: 0);
          }),
    ];
  }

  Widget _buildOptionWidget(FilterOptionEntity o) {
    return GestureDetector(
      onTap: () => select(o),
      child: Container(
        margin: EdgeInsets.only(right: o.index.isEven ? R.dimen.dp32 : 0),
        child: Column(
          children: [
            o.icon.isEmpty
                ? Text(
                    o.label,
                    style: o.index == currentIndex
                        ? TextStyle(color: R.color.ff33b18c)
                        : null,
                  )
                : RotateButton(
                    onPressed: () {
                      return Future.value(o.onTap!());
                    },
                    label: Text(
                      o.label + "  ",
                      style: o.index == currentIndex
                          ? TextStyle(color: R.color.ff33b18c)
                          : null,
                    ),
                  ),
            Container(
              margin: EdgeInsets.only(top: R.dimen.dp7),
              width: R.dimen.dp15,
              height: R.dimen.dp4,
              decoration: BoxDecoration(
                  color: currentIndex == o.index
                      ? R.color.ff33b18c
                      : R.color.transparent,
                  borderRadius: BorderRadius.circular(R.dimen.sp2)),
            )
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        showModalPopdown(context, builder: (BuildContext ctx) {
          return Text("---");
        }, settings: RouteSettings());
      },
      child: Container(
          child: DefaultTextStyle(
        style: TextStyle(fontSize: 12, color: const Color(0xFF333333)),
        child: Row(children: [
          Expanded(
              child: Row(
            children: [
              for (FilterOptionEntity o in options1) _buildOptionWidget(o)
            ],
          )),
          Expanded(
              child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              for (FilterOptionEntity o in options2) _buildOptionWidget(o)
            ],
          )),
        ]),
      )),
    );
  }
}
